<template>
	<view>
		 <!-- 小黑框 -->
		 <view style="position: relative; z-index: 9999999999">
            <toast id="toast" ref="toasts"></toast>
        </view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				wxmini:'',
				ordernum:'',
				rewardedVideoAd:'',
				closeAdv:false,
				isload:false,
			}
		},
		onLoad(options) {
			const that = this;
			console.log('onLoad')
			that.wxmini = options.wxmini
			that.ordernum = options.ordernum;
			that.payPhone_entrance = options.payPhone_entrance;
			if(!that.closeAdv){
				that.initRewardAdv()
			}
		},
		onUnload(){
			const that = this;
			console.log('onUnload')
			if(that.rewardedVideoAd){
				that.rewardedVideoAd.destory();
				that.rewardedVideoAd = null;
			}
		},
		methods: {
			// 初始化广告
			initRewardAdv(){
				const that = this;
				if(that.rewardedVideoAd) return false; //已经初始化
				if(wx.createRewardedVideoAd){
					uni.showLoading({
						title:'广告加载中'
					})
					that.rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: that.wxmini })
					that.rewardedVideoAd.onLoad(() => {
						uni.hideLoading()
						if(!that.isload){
							that.rewardedVideoAd.show()
							that.isload = true;
						}
					})
					that.rewardedVideoAd.onError((err) => {
						console.log('onError event emit', err)
						uni.hideLoading()
						that.updatePlayState(2)
					})
					that.rewardedVideoAd.offClose();
					that.rewardedVideoAd.onClose((res) => {
						console.log('onClose event emit', res)
						uni.hideLoading()
						// 用户点击了【关闭广告】按钮
						if (res && res.isEnded) {
							// that.showRewardPop = false; //关闭弹窗
						// 正常播放结束，可以下发游戏奖励
							that.updatePlayState(1)
						} else {
						// 播放中途退出，不下发游戏奖励
							that.rewardedVideoAd.destory();
							that.rewardedVideoAd = null;
							setTimeout(() => {
								uni.navigateBack({
									delta: 1,
									fail:function(res){
										let pages = getCurrentPages()
										let options = pages[pages.length - 2].options
										let param = [];
										for(let i in options){
											if(i == 'url'){
												let url_param = decodeURIComponent(options[i])
												let str = encodeURIComponent(url_param.replace('?','huoniaowh').replace(/\&/g, 'huoniaolj').replace(/\=/g, 'huoniaodh'))
												param.push(i + '=' + str)
											}else{
												param.push(i + '=' + options[i])
											}
										}
										uni.reLaunch({ url: `/${pages[pages.length - 2].route}?${param.join('&')}`})
									},
									success:function(){
										console.log('success')
									}
								});
							}, 100);

							
							

							
						}
					})
				}
			},


			// 告诉服务端 播放完毕
			updatePlayState(playState){
				const that = this;
				uni.showLoading({
					title:'加载中...'
				})
				uni.login({
					success:res => {
						var code = res.code;
						var dataPrarm = 'service=siteConfig&action=wxminiAdRewardVideoNotify&ordernum=' + that.ordernum+'&code=' + code + '&state=' + playState;
						var urlData = app.encodeUrl(dataPrarm);
						that.$ajax.post({
							url: 'include/ajax.php?' + urlData,
						}).then(res => {
							if(playState == 2){
								let tip = that.payPhone_entrance == '0' || that.payPhone_entrance == 0 ? '广告拉取失败，请稍后重试':'广告拉取失败，请支付后查看或者稍后重试'
								that.$refs.toasts.showToast(tip)
								console.log('开始3')
							}else{
								console.log(that.ordernum)
								that.closeAdv = true;
								that.rewardedVideoAd.destory();
								that.rewardedVideoAd = null;
							
							}
							uni.hideLoading()
							
							// 此处注释原因是：uni.navigateBack失败之后 跳转的页面 左上角返回按钮不会生效 
							uni.navigateBack({
								delta: 1,
								fail:function(res){
									let pages = getCurrentPages()
									let options = pages[pages.length - 2].options
									let param = [];
									for(let i in options){
										if(i == 'url'){
											let ordernum_str = 'ordernum=' + that.ordernum
											let url_param = decodeURIComponent(options[i])
											if(url_param.indexOf('?') != -1){
												url_param = url_param + '&' + ordernum_str
											}else{
												url_param = url_param + '?' + ordernum_str
											}
											let str = encodeURIComponent(url_param.replace('?','huoniaowh').replace(/\&/g, 'huoniaolj').replace(/\=/g, 'huoniaodh'))
											param.push(i + '=' + str)
										}else{
											param.push(i + '=' + options[i])
										}
									}
									uni.reLaunch({ url: `/${pages[pages.length - 2].route}?${param.join('&')}`})
								},
							});
								
						})
					}
				})
			},
		}
	}
</script>

<style>

</style>
